package com.open.free.generator.project;

import com.open.free.generator.constant.TemplateConstants;
import com.open.free.generator.utils.FileIOUtils;
import com.open.free.generator.utils.FreemarkerUtils;
import freemarker.template.TemplateException;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Faye.Wang
 * @version 1.0
 * @date 2020/12/20 21:31
 * @Description
 */
@Component
public class MavenWrapper {
    @Autowired
    private FreemarkerUtils freemarkerUtils;

    /**
     * 根据maven版本号进行maven 文件信息配置。生成 springboot maven配置信息
     * @param mavenVersion
     * @param mavenWrapper
     * @param targetPath
     * @throws IOException
     * @throws TemplateException
     */
    public void mavenWrapperGenerator(String mavenVersion,String mavenWrapper,String targetPath) throws IOException, TemplateException {

        Map<String,Object> dataMaps = new HashMap<>();
        dataMaps.put("mavenVersion",mavenVersion);
        dataMaps.put("mavenWrapper",mavenWrapper);
        String properties =  freemarkerUtils.processTemplateIntoString(TemplateConstants.MAVEN_WRAPPER_PROPERTIES,dataMaps);
        FileIOUtils.createFile(properties,targetPath,"maven-wrapper.properties");


        String downloader =  freemarkerUtils.processTemplateIntoString(TemplateConstants.MAVEN_WRAPPER_DOWNLOADER,dataMaps);
        FileIOUtils.createFile(downloader,targetPath,"MavenWrapperDownloader"+TemplateConstants.JAVA_FILE_SUFFIX);

        ClassPathResource reJar = new ClassPathResource(TemplateConstants.MAVEN_WRAPPER_JAR);
        FileIOUtils.copyFileToPath(reJar.getFile(),targetPath);

    }
}
